import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { resourceType } = query;

  // RBO行为定义数据
  const behaviorDefs = [
    {
      id: 'reserve_room',
      resourceType: 'room',
      name: '预订房间',
      inputSchema: {
        type: 'object',
        properties: {
          guestName: { type: 'string' },
          checkInDate: { type: 'string', format: 'date' },
          checkOutDate: { type: 'string', format: 'date' },
          roomType: { type: 'string' },
        },
        required: ['guestName', 'checkInDate', 'checkOutDate'],
      },
      outputSchema: {
        type: 'object',
        properties: {
          reservationId: { type: 'string' },
          status: { type: 'string' },
          roomNumber: { type: 'string' },
        },
      },
      preconditions: [
        { type: 'stateIs', value: ['idle'] },
        { type: 'roleHas', value: ['frontdesk', 'channelPartner'] },
      ],
      sideEffects: {
        emits: ['RoomReserved'],
        transitions: {
          from: ['idle'],
          to: 'reserved',
        },
      },
      impl: {
        type: 'bpm',
        ref: 'PROC_ROOM_RESERVE',
        timeoutSec: 60,
      },
      sla: {
        retry: 1,
        compensation: 'PROC_ROOM_RESERVE_COMPENSATION',
      },
    },
    {
      id: 'checkin_room',
      resourceType: 'room',
      name: '入住房间',
      inputSchema: {
        type: 'object',
        properties: {
          reservationId: { type: 'string' },
          guestId: { type: 'string' },
          checkInTime: { type: 'string' },
        },
        required: ['reservationId', 'guestId'],
      },
      outputSchema: {
        type: 'object',
        properties: {
          checkInId: { type: 'string' },
          status: { type: 'string' },
          roomKey: { type: 'string' },
        },
      },
      preconditions: [
        { type: 'stateIs', value: ['reserved'] },
        { type: 'roleHas', value: ['frontdesk'] },
      ],
      sideEffects: {
        emits: ['RoomCheckedIn'],
        transitions: {
          from: ['reserved'],
          to: 'occupied',
        },
      },
      impl: {
        type: 'service',
        ref: 'checkin-service',
        timeoutSec: 30,
      },
      sla: {
        retry: 2,
        compensation: 'PROC_CHECKIN_COMPENSATION',
      },
    },
    {
      id: 'checkout_room',
      resourceType: 'room',
      name: '退房',
      inputSchema: {
        type: 'object',
        properties: {
          checkInId: { type: 'string' },
          checkOutTime: { type: 'string' },
          paymentMethod: { type: 'string' },
        },
        required: ['checkInId'],
      },
      outputSchema: {
        type: 'object',
        properties: {
          checkoutId: { type: 'string' },
          totalAmount: { type: 'number' },
          status: { type: 'string' },
        },
      },
      preconditions: [
        { type: 'stateIs', value: ['occupied'] },
        { type: 'roleHas', value: ['frontdesk'] },
      ],
      sideEffects: {
        emits: ['RoomCheckedOut'],
        transitions: {
          from: ['occupied'],
          to: 'cleaning',
        },
      },
      impl: {
        type: 'orchestration',
        ref: 'checkout-orchestration',
        timeoutSec: 120,
      },
      sla: {
        retry: 1,
        compensation: 'PROC_CHECKOUT_COMPENSATION',
      },
    },
  ];

  // 按资源类型过滤
  let filteredBehaviors = behaviorDefs;
  if (resourceType) {
    filteredBehaviors = behaviorDefs.filter(
      (behavior) => behavior.resourceType === resourceType,
    );
  }

  return {
    code: 0,
    data: filteredBehaviors,
    message: '获取行为定义成功',
  };
});
